package com.cdut.firstspringdemo.controller;

import com.cdut.firstspringdemo.entity.Information;
import com.cdut.firstspringdemo.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

@Controller
public class InformationController {
    private static File informationDir;
    static {
        informationDir = new File(("./information"));
        if (!informationDir.exists()) {
            informationDir.mkdirs();
        }
    }

    @RequestMapping("deleteInformation")
    public void delete(HttpServletRequest request, HttpServletResponse response){
        System.out.println("开始删除信息");
        String title = request.getParameter("title");
        System.out.println("要删除的信息是"+title);
        File file = new File(informationDir,title+".obj");
        file.delete();

        try {
            response.sendRedirect("/informationList");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @RequestMapping("/informationList")
    public void informationList(HttpServletRequest request,HttpServletResponse response){
        List<Information> informationList = new ArrayList<>();
        File[] subs = informationDir.listFiles(f -> f.getName().endsWith(".obj"));
        for(File sub : subs){
            try( FileInputStream fis = new FileInputStream(sub);
                 ObjectInputStream ois = new ObjectInputStream(fis)
            ){
                Information information = (Information) ois.readObject();
                informationList.add(information);

            } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
        response.setContentType("text/html;charset=utf-8");
        try {
            PrintWriter pw = response.getWriter();
            pw.println("<!DOCTYPE html>");
            pw.println("<html lang=\"en\">");
            pw.println("<head>");
            pw.println("<meta charset=\"UTF-8\">");
            pw.println("<title>文章列表</title>");
            pw.println("</head>");
            pw.println("<body>");
            pw.println("<center>");
            pw.println("<h1>文章列表</h1>");
            pw.println("<table border=\"2\">");
            pw.println("<tr>");
            pw.println("<td>标题</td>");
            pw.println("<td>作者</td>");
            pw.println("<td>操作</td>");
            pw.println("<tr>");

            for (Information information : informationList){
                pw.println("<tr>");
                pw.println("<td>"+information.getTitle()+"</td>");
                pw.println("<td>"+information.getAuthor()+"</td>");
                pw.println("<td><a href='/deleteInformation?title="+information.getTitle()+"'>删除</a></td>");
                pw.println("<tr>");
            }
            pw.println("</center>");
            pw.println("</body>");
            pw.println("</html>");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @RequestMapping("/writeInformation")
    public void writeInformation(HttpServletRequest request, HttpServletResponse response) {
        String title = request.getParameter("title");
        String author = request.getParameter("author");
        String content = request.getParameter("content");
        if (title == null || title.trim().isEmpty() ||
                author == null || author.trim().isEmpty() ||
                content == null || content.trim().isEmpty()) {
            try {
                response.sendRedirect("/information_fail.html");
            } catch (IOException e) {
                e.printStackTrace();
            }
            return;
        }
        Information information = new Information(title,author,content);
        File file = new File(informationDir,title+".obj");
        try(FileOutputStream fos = new FileOutputStream(file);
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            ) {
            oos.writeObject(information);
            response.sendRedirect("/information_success.html");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

